home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / sprocket / experimentalstuff / document.cp next >
Encoding:
Text File  |  1996-01-02  |  4.1 KB  |  241 lines

  1. /*
  2.     File:        Document.cp
  3.  
  4.     Contains:    An abstract document class
  5.  
  6.     Written by: Dave Falkenburg
  7.  
  8.     Copyright:    © 1994-95 by Dave Falkenburg, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <1>     1/24/95    DRF        First checked in.
  13.     To Do:        Integrate Marshall’s printing loop
  14.                 Mix-in Eric’s MAEObject to make things scriptable    
  15.  */
  16.  
  17. #include "Document.h"
  18.  
  19. #include "Sprocket.h"
  20. #include <TextUtils.h>
  21.  
  22.  
  23. //    internal function prototypes and UPPs:
  24.  
  25. static    pascal Boolean    CloseDialogFilterProc(DialogPtr theDialog, EventRecord* anEvent, short* itemHit);
  26. static    ModalFilterUPP    CloseDialogFilterUPP = NewModalFilterProc(CloseDialogFilterProc);
  27.  
  28.  
  29. TDynamicArray    TDocument::fgActiveDocuments;
  30.  
  31.  
  32. TDocument::TDocument(LetterDescriptor * theContainer /* = NULL */)
  33.     {
  34.     fgActiveDocuments.InsertFirst(this);
  35.  
  36.     if (theContainer)
  37.         {
  38.         fNeedsSave = false;
  39.         fHasBeenSaved = true;
  40.         fHasNewEditions = false;
  41.         }
  42.     else
  43.         {
  44.         fNeedsSave = true;
  45.         fHasBeenSaved = false;
  46.         fHasNewEditions = false;
  47.         }
  48.     }
  49.  
  50.     
  51. TDocument::~TDocument()
  52.     {
  53.     fgActiveDocuments.Delete(this);
  54.     }
  55.  
  56.  
  57. Boolean
  58. TDocument::DoMenuCommand(MenuCommandID command)
  59.     {
  60.     switch (command)
  61.         {
  62.         case    cClose:
  63.             this->Close(false);
  64.             break;
  65.  
  66.         case    cSave:
  67.             if (fHasBeenSaved)
  68.                 this->Save();
  69.             else
  70.                 this->SaveAs();
  71.             break;
  72.             
  73.         case    cSaveAs:
  74.             this->SaveAs();
  75.             break;
  76.             
  77.         case    cPageSetup:
  78.         case    cCustomPageSetup:
  79.             this->PageSetup(command == cCustomPageSetup);
  80.             break;
  81.         
  82.         case    cPrint:
  83.         case    cPrintOne:
  84.             this->Print(command == cPrint);
  85.             break;
  86.  
  87.         default:
  88.             return false;
  89.         }
  90.  
  91.     return true;
  92.     }
  93.  
  94.  
  95. StringPtr
  96. TDocument::GetDocumentName(void)
  97.     {
  98.     return "\pUntitled";
  99.     }
  100.  
  101.     
  102. Boolean
  103. TDocument::Close(Boolean quitting)
  104.     {
  105.     OSErr    err = noErr;
  106.     
  107.     if (fNeedsSave)
  108.         {
  109.         CloseResult    whatShouldWeDo = this->CloseDialog(quitting);
  110.         
  111.         if (whatShouldWeDo == kCancelCloseDocument)
  112.             return false;
  113.  
  114.         else if (whatShouldWeDo != kDontSaveDocument)
  115.             {
  116.             if (fHasBeenSaved)
  117.                 err = this->Save();
  118.             else
  119.                 err = this->SaveAs();
  120.             }            
  121.         }
  122.  
  123.     if (err == noErr)    // Only allow to close if we successfully saved!
  124.         {
  125.         delete this;        //    Get rid of us, and our little windows, too!
  126.         return true;
  127.         }
  128.     
  129.     return false;
  130.     }
  131.  
  132.  
  133. //////////////////////////////////////////////////////////////////
  134. //
  135. //    CloseDialog
  136. //
  137. //    Provides the standard human interface for closing a document
  138.  
  139. TDocument::CloseResult
  140. TDocument::CloseDialog(Boolean quitting)
  141.     {
  142.     StringPtr    nullStr = (StringPtr) "\p";
  143.     Str255        reasonForClosingStr;
  144.     short        whichAlert;
  145.     short        whichString;
  146.  
  147.     if (fHasNewEditions)
  148.         whichAlert = kStandardCloseWithNewPubsAlertID;
  149.     else
  150.         whichAlert = kStandardCloseAlertID;
  151.     
  152.     if (quitting)
  153.         whichString = kQuittingStr;
  154.     else
  155.         whichString = kClosingStr;
  156.  
  157.     GetIndString(reasonForClosingStr,kStandardCloseStrings,whichString);
  158.     ParamText(GetDocumentKind(),GetDocumentName(),reasonForClosingStr,nullStr);
  159.     
  160.     return ((CloseResult) StandardAlert(whichAlert,kSaveDocument,kCancelCloseDocument,CloseDialogFilterUPP));
  161.     }
  162.  
  163.  
  164. static pascal Boolean
  165. CloseDialogFilterProc(DialogPtr theDialog, EventRecord* anEvent, short* itemHit)
  166.     {
  167.     if ((anEvent->what == keyDown))
  168.         {
  169.         char    c = (char) anEvent->message & charCodeMask;
  170.         
  171.         if ((c == 'd') || (c == 'D'))        //    NOT INTERNATIONAL FRIENDLY!!!
  172.             {
  173.             *itemHit = TDocument::kDontSaveDocument;
  174.             PseudoClickInDialogItem(theDialog,*itemHit);
  175.             return true;
  176.             }
  177.         }
  178.  
  179.     //    Return through the common code above so that default item processing can happen
  180.     return StandardDialogFilterProc(theDialog, anEvent, itemHit);
  181.     }
  182.  
  183.  
  184. /* static */ Boolean
  185. TDocument::CloseAllDocuments()
  186.     {
  187.     TDocument    *aDocument;
  188.     
  189.     while ((aDocument = (TDocument *) fgActiveDocuments.First()) != NULL)
  190.         {
  191.         if (aDocument->Close(true) == false)
  192.             break;
  193.         }
  194.  
  195.  
  196.     //    If we got through all the documents, return true
  197.     return (aDocument == NULL);
  198.     }
  199.  
  200.  
  201. void
  202. TDocument::BringDocumentToFront()
  203.     {
  204.     fgActiveDocuments.MoveToFront((ArrayElementPtr) this);
  205.     }
  206.  
  207.  
  208. OSErr
  209. TDocument::Save()
  210.     {
  211.     return -1;
  212.     }
  213.  
  214.  
  215. OSErr
  216. TDocument::SaveAs()
  217.     {
  218.     return -1;
  219.     }
  220.  
  221.  
  222. OSErr
  223. TDocument::Revert()
  224.     {
  225.     return -1;
  226.     }
  227.  
  228.  
  229. //    printing methods
  230.  
  231. void
  232. TDocument::PageSetup(Boolean /* useCustomPageSetup */)
  233.     {
  234.     }
  235.  
  236.  
  237. void
  238. TDocument::Print(Boolean /* usePrintJobDialog */)
  239.     {
  240.     }
  241.